package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;

@Service
public class SetmealServiceImpl implements SetmealService {

    @Resource
    SetmealMapper setmealMapper;
    @Resource
    SetmealDishMapper setmealDishMapper;
    @Resource
    DishServiceImpl dishService;
    @Override
    public PageResult page(SetmealPageQueryDTO setmealPageQueryDTO) {
        //设置分页数据
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());

        Page<SetmealVO> page = setmealMapper.page(setmealPageQueryDTO);

        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public void addSetmeal(SetmealDTO setmealDTO) {
        //新增数据，将dto的数据写如entity
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        //新增setmealMapper数据
        setmealMapper.inseat(setmeal);

        //获取套餐id
        Long setmealId = setmeal.getId();
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        //循环为其赋值id绑定关系
        if (setmealDishes != null && setmealDishes.size()>0) {
            setmealDishes.forEach(setmealDish -> {
                setmealDish.setSetmealId(setmealId);
            });
            setmealDishMapper.inseat(setmealDishes);
        }


    }

    @Override
    public void deletesetmeal(List<Long> ids) {
        //判断是否在售
        for (Long id : ids) {
           Setmeal setmeal = setmealMapper.select(id);
            if (setmeal.getStatus() == StatusConstant.ENABLE)
            {
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        }
        setmealMapper.deletesetmeal(ids);
        setmealDishMapper.deletesetmealdish(ids);
    }

    @Override
    public SetmealVO selectById(Long id) {
        //查套餐
        Setmeal setmeal =  setmealMapper.selectById(id);
        //查菜品
        List<SetmealDish> setmealDishes= setmealDishMapper.selectById(id);
        //组装数据
        SetmealVO setmealVO =new SetmealVO();
        BeanUtils.copyProperties(setmeal,setmealVO);
        setmealVO.setSetmealDishes(setmealDishes);
        //返回数据
        return setmealVO;
    }

    @Override
    public void status(Integer status, Long id) {
        Setmeal setmeal = setmealMapper.selectById(id);
        Long setmealId = setmeal.getId();
        List<SetmealDish> setmealDishes = setmealDishMapper.selectById(setmealId);
        setmealDishes.forEach(setmealDish -> {
            Long dishId = setmealDish.getDishId();
            dishService.updateStatus(String.valueOf(status),dishId);
        });
        setmealMapper.status(status,id);
    }

    @Override
    public void updateSetmeal(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        //修改套餐
        setmealMapper.updateSetmeal(setmeal);

        //删除setmealdish中数据
        setmealDishMapper.deletesetmealdish(Collections.singletonList(setmealDTO.getId()));
        //插入setmealdish数据
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        if (setmealDishes != null && setmealDishes.size() > 0) {
            setmealDishes.forEach(setmealDish -> {
                 setmealDish.setId(setmealDTO.getCategoryId());
            });
            setmealDishMapper.inseat(setmealDishes);
        }

    }


    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }
}
